﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Management.Automation;
using MayhemWpf.UserControls;
using System.ComponentModel;
using Microsoft.Win32;

namespace MayhemPowerShell
{
    /// <summary>
    /// Interaction logic for CommandConfig.xaml
    /// </summary>
    public partial class ScriptConfig : WpfConfiguration
    {
        public override string Title
        {
            get
            {
                return "PowerShell Command";
            }
        }

        public String ScriptText
        {
            get
            {
                return _scriptText;
            }
            private set
            {
                _scriptText = value;
                scriptTextBox.Text = _scriptText;
            }
        }
        public bool UseFile
        {
            get
            {
                return _useFile;
            }
            private set
            {
                _useFile = value;

                scriptFileBox.IsEnabled = value;
                browseButton.IsEnabled = value;
                scriptTextBox.IsEnabled = !value;
            }
        }
        public String FilePath
        {
            get
            {
                return _scriptFile;
            }
            private set
            {
                _scriptFile = value;
                scriptFileBox.Text = _scriptFile;
            }
        }

        private String _scriptText, _scriptFile;
        private bool _useFile;

        public ScriptConfig() : this("", false, "")
        {
        }

        public ScriptConfig(String scriptText, bool useFile, String scriptFile)
        {
            InitializeComponent();

            _scriptText = scriptText;
            _useFile = useFile;
            _scriptFile = scriptFile;

            CanSave = true;
        }

        private String getExecutionPolicy()
        {
            PowerShell shell = PowerShell.Create();

            return shell.ExecuteScript<String>("Get-ExecutionPolicy").FirstOrDefault();
        }

        private void enterTextRadio_Checked(object sender, RoutedEventArgs e)
        {
            UseFile = false;
        }

        private void useFileRadio_Checked(object sender, RoutedEventArgs e)
        {
            UseFile = true;
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter += "PS1 Files|*.ps1";
            var outcome = dlg.ShowDialog();

            if (outcome.HasValue && outcome.Value)
            {
                String file = dlg.FileName;
                FilePath = file;
            }
        }

        public override void OnSave()
        {
            ScriptText = scriptTextBox.Text;
            FilePath = scriptFileBox.Text;
        }

        public override void OnLoad()
        {
            if (UseFile)
            {
                UseFile = true;
                scriptFileBox.Text = FilePath;
            }
            else
            {
                UseFile = false;
                scriptTextBox.Text = ScriptText;
            }
        }
    }
}
